package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    RoleService roleService;


    @RequestMapping("/deleteRoleById")
    public Result deleteRoleById(int id) {

        try {
            roleService.deleteRoleById(id);
            return new Result(true, "删除角色成功");
        } catch (RuntimeException r) {
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "未知错误");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> map) {
       try{
           JSONObject jsonObject = (JSONObject) map.get("formData");
        //JSONObject包json串转换为对象
        Role role = jsonObject.toJavaObject(Role.class);

        JSONArray jsonArray = (JSONArray) map.get("permissionIds");
        //JSONArray包json串转换为数组对象
        List<Integer> permissionIds = jsonArray.toJavaList(Integer.class);

        JSONArray jsonArray2 = (JSONArray) map.get("menuIds");
        //JSONArray包json串转换为数组对象
        List<Integer> menuIds = jsonArray2.toJavaList(Integer.class);

        JSONArray jsonArray3 = (JSONArray) map.get("menudetailsIds");
        //JSONArray包json串转换为数组对象
        List<Integer> menudetailsIds = jsonArray3.toJavaList(Integer.class);
        Map<String,Object> mapdata= new HashMap<>();
        mapdata.put("role",role);
        mapdata.put("permissionIds",permissionIds);
        mapdata.put("menuIds",menuIds);
        mapdata.put("menudetailsIds",menudetailsIds);

            roleService.edit(mapdata);
            return new Result(true, "编辑角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑角色失败");
        }
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //传入当前页 每页个数 查询条件  service 用分页包 分页
        PageResult pageResult=roleService.findPage(queryPageBean);
        //返回 当页查到的数据  总数据个数
        return pageResult;
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map){
        JSONObject jsonObject = (JSONObject) map.get("formData");
        //JSONObject包json串转换为对象
        Role role = jsonObject.toJavaObject(Role.class);

        JSONArray jsonArray = (JSONArray) map.get("permissionIds");
        //JSONArray包json串转换为数组对象
        List<Integer> permissionIds = jsonArray.toJavaList(Integer.class);

        JSONArray jsonArray2 = (JSONArray) map.get("menuIds");
        //JSONArray包json串转换为数组对象
        List<Integer> menuIds = jsonArray2.toJavaList(Integer.class);

        JSONArray jsonArray3 = (JSONArray) map.get("menudetailsIds");
        //JSONArray包json串转换为数组对象
        List<Integer> menudetailsIds = jsonArray3.toJavaList(Integer.class);
        Map<String,Object> mapdata= new HashMap<>();
        mapdata.put("role",role);
        mapdata.put("permissionIds",permissionIds);
        mapdata.put("menuIds",menuIds);
        mapdata.put("menudetailsIds",menudetailsIds);

        try {
            //传入数据  选中各项的id  和表单数据
            roleService.add(mapdata);
            return new Result(true, "添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色失败");
        }

    }



    @RequestMapping("/findRoleById")
    public Result findRoleById(int id){
        try {
            Map<String,Object> map = roleService.findRoleById(id);
            return new Result(true,"查询角色成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"未知错误");
        }
    }


}
